package com.huahan.auth.granter;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huahan.auth.domain.UserInfo;
import com.huahan.auth.exception.BusinessException;
import com.huahan.auth.mapper.UserInfoMapper;
import com.huahan.auth.security.component.HuahanUserDetail;
import com.mall.common.constant.CommonConstant;
import com.mall.common.constant.LoginTypeEnum;
import com.mall.common.constant.ResultCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.provider.*;
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.*;



public class AppTokenGranter extends AbstractTokenGranter {
    /***************************************************************
     *                                                             *
     * ***********************2-PC授权************************
     *                                                             *
     * *************************************************************/
    private UserInfoMapper userInfoDao;
  public AppTokenGranter(AuthorizationServerTokenServices tokenServices,
                         ClientDetailsService clientDetailsService,
                         OAuth2RequestFactory requestFactory) {
    super(tokenServices, clientDetailsService, requestFactory, LoginTypeEnum.PC_CODE.getValue());
  }

  public void setAccountService(UserInfoMapper userInfoDao) {
    this.userInfoDao = userInfoDao;
  }

    @Override
  protected OAuth2Authentication getOAuth2Authentication(ClientDetails client,
      TokenRequest tokenRequest) {
    Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());
        List<GrantedAuthority> grantedAuthorities = new ArrayList();
        grantedAuthorities.add(new SimpleGrantedAuthority("USER"));
        // 客户端提交的手机号码
        String phoneNumber = parameters.get(CommonConstant.DEFAULT_PARAMETER_NAME_PHONE);
        if (StringUtils.isBlank(phoneNumber)) {
            throw new BusinessException(ResultCode.PHONE_NUMBER_IS_EMPTY);
        }
        // 客户端提交的验证码
        String appcode = parameters.get(CommonConstant.DEFAULT_PARAMETER_NAME_PWD);
        if (StringUtils.isBlank(appcode)) {
            throw new BusinessException(ResultCode.PASSWORD_IS_EMPTY);
        }
        // 客户端提交的手机号码 根据手机号码查询用户
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("phone_number",phoneNumber);
        // pc 授权登录
        if("1".equals(parameters.get("flag"))){
            userInfoQueryWrapper.eq("is_pc","1");
        }
        if("2".equals(parameters.get("flag"))){
            userInfoQueryWrapper.eq("is_app","1");
        }
        List<UserInfo> list = userInfoDao.selectList(userInfoQueryWrapper);
        UserInfo user = null;
        if(list.size()>0){
            user = list.get(0);
        }
        HuahanUserDetail userDetails = new HuahanUserDetail(user,grantedAuthorities);
        // 验证用户状态
        AbstractAuthenticationToken userAuth = new UsernamePasswordAuthenticationToken(user, null, userDetails.getAuthorities());
        parameters.put("empType",String.valueOf(user.getEmpType()));
        userAuth.setDetails(parameters);
        OAuth2Request oAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);

        return new OAuth2Authentication(oAuth2Request, userAuth);

  }

}
